package com.bmd.backgroundmanger.controller;

import com.bmd.backgroundmanger.commons.Result;
import com.bmd.backgroundmanger.entity.ExamPaper;
import com.bmd.backgroundmanger.service.IExamPaperService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 试卷管理 前端控制器
 * </p>
 *
 * @author 刷题App
 * @since 2024-12-19
 */
@RestController
@RequestMapping("/paper")
@Tag(name = "试卷管理模块")
public class ExamPaperController {

    @Resource
    private IExamPaperService examPaperService;

    @PostMapping("/selectPageData")
    @Operation(summary = "分页查询试卷列表")
    public Result<Map<String, Object>> selectPageData(@RequestBody Map<String, Object> params) {
        // 从请求体中获取参数
        Integer page = (Integer) params.get("page");
        Integer size = (Integer) params.get("size");
        String title = (String) params.get("title");
        String status = (String) params.get("status");
        // 设置默认值
        if (page == null) page = 1;
        if (size == null) size = 10;
        
        Map<String, Object> data = examPaperService.getPageData(page, size, title, status);
        return Result.success(data);
    }

    @PostMapping("/create")
    @Operation(summary = "创建试卷")
    public Result<String> createPaper(@RequestBody ExamPaper examPaper) {
        boolean success = examPaperService.createPaper(examPaper);
        return success ? Result.success("试卷创建成功!") : Result.error(500, "试卷创建失败!");
    }

    @PostMapping("/update")
    @Operation(summary = "更新试卷")
    public Result<String> updatePaper(@RequestBody ExamPaper examPaper) {
        boolean success = examPaperService.updatePaper(examPaper);
        return success ? Result.success("试卷更新成功!") : Result.error(500, "试卷更新失败!");
    }

    @PostMapping("/save")
    @Operation(summary = "新增/编辑试卷")
    public Result<String> saveOrUpdate(@RequestBody ExamPaper examPaper) {
        boolean success;
        if (examPaper.getPaperId() == null) {
            // 新增
            success = examPaperService.createPaper(examPaper);
            return success ? Result.success("试卷创建成功!") : Result.error(500, "试卷创建失败!");
        } else {
            // 编辑
            success = examPaperService.updatePaper(examPaper);
            return success ? Result.success("试卷更新成功!") : Result.error(500, "试卷更新失败!");
        }
    }

    @GetMapping("/detail/{paperId}")
    @Operation(summary = "获取试卷详情")
    public Result<ExamPaper> getPaperDetail(@PathVariable Integer paperId) {
        ExamPaper paper = examPaperService.getPaperWithQuestions(paperId);
        return paper != null ? Result.success(paper) : Result.error(404, "试卷不存在!");
    }

    @PostMapping("/addQuestion")
    @Operation(summary = "向试卷添加题目")
    public Result<String> addQuestionToPaper(
            @RequestParam Integer paperId,
            @RequestParam Integer questionId,
            @RequestParam(value = "score", required = false) Integer score) {
        
        boolean success = examPaperService.addQuestionToPaper(paperId, questionId, score);
        return success ? Result.success("题目添加成功!") : Result.error(500, "题目添加失败或已存在!");
    }

    @PostMapping("/addQuestions")
    @Operation(summary = "批量向试卷添加题目")
    public Result<String> addQuestionsToPaper(
            @RequestParam Integer paperId,
            @RequestBody List<Integer> questionIds) {
        
        boolean success = examPaperService.addQuestionsToPaper(paperId, questionIds);
        return success ? Result.success("题目批量添加成功!") : Result.error(500, "题目批量添加失败!");
    }

    @DeleteMapping("/removeQuestion")
    @Operation(summary = "从试卷移除题目")
    public Result<String> removeQuestionFromPaper(
            @RequestParam Integer paperId,
            @RequestParam Integer questionId) {
        
        boolean success = examPaperService.removeQuestionFromPaper(paperId, questionId);
        return success ? Result.success("题目移除成功!") : Result.error(500, "题目移除失败!");
    }

    @PostMapping("/publish/{paperId}")
    @Operation(summary = "发布试卷")
    public Result<String> publishPaper(@PathVariable Integer paperId) {
        boolean success = examPaperService.publishPaper(paperId);
        return success ? Result.success("试卷发布成功!") : Result.error(500, "试卷发布失败!");
    }

    @PostMapping("/archive/{paperId}")
    @Operation(summary = "归档试卷")
    public Result<String> archivePaper(@PathVariable Integer paperId) {
        boolean success = examPaperService.archivePaper(paperId);
        return success ? Result.success("试卷归档成功!") : Result.error(500, "试卷归档失败!");
    }

    @DeleteMapping("/delete/{paperId}")
    @Operation(summary = "删除试卷")
    public Result<String> deletePaper(@PathVariable Integer paperId) {
        boolean success = examPaperService.removeById(paperId);
        return success ? Result.success("试卷删除成功!") : Result.error(500, "试卷删除失败!");
    }

    @PostMapping("/deleteBatch")
    @Operation(summary = "批量删除试卷")
    public Result<String> deletePapersBatch(@RequestBody List<Integer> paperIds) {
        boolean success = examPaperService.removeBatchByIds(paperIds);
        return success ? Result.success("试卷批量删除成功!") : Result.error(500, "试卷批量删除失败!");
    }

    @GetMapping("/published")
    @Operation(summary = "获取已发布的试卷列表")
    public Result<List<ExamPaper>> getPublishedPapers() {
        List<ExamPaper> papers = examPaperService.getPublishedPapers();
        return Result.success(papers);
    }

    @PostMapping("/autoCompose")
    @Operation(summary = "自动组卷")
    public Result<String> autoComposePaper(
            @RequestParam Integer paperId,
            @RequestParam(value = "categoryId", required = false) Integer categoryId,
            @RequestParam(value = "difficultyLevel", required = false) String difficultyLevel,
            @RequestParam(value = "questionCount", required = false) Integer questionCount) {
        
        boolean success = examPaperService.autoComposePaper(paperId, categoryId, difficultyLevel, questionCount);
        return success ? Result.success("自动组卷成功!") : Result.error(500, "自动组卷失败!");
    }

    @PostMapping("/adjustOrder")
    @Operation(summary = "调整题目顺序")
    public Result<String> adjustQuestionOrder(
            @RequestParam Integer paperId,
            @RequestBody List<Map<String, Integer>> orderList) {
        
        boolean success = examPaperService.adjustQuestionOrder(paperId, orderList);
        return success ? Result.success("题目顺序调整成功!") : Result.error(500, "题目顺序调整失败!");
    }
} 